<?php
namespace interface\v1;
require_once '../../control/interface.php';
require_once '../../control/verify.php';
use control\interfaces as API;
class wechatInitClass
{
    use API;
    /**
     * 首次进入小程序时需要关联企业和用户
     */
    public function setUser()
    {
        $corpname = $this->post('corpname');
        $uid = $this->post('uid');//其他企业的人员请求雨木科技企业产生的加密userId
        if (empty($corpname)) {
            $this->resp['warn'] = '请填写公司名称';
        } elseif (empty($uid)) {
            $this->resp['warn'] = '未找到uid';
        } else {
            $company = query('company', "shortTitle = '$corpname'");

            if (empty($company['coid'])) {
                $this->resp['warn'] = '未找到此公司';
            } elseif (strtotime($company['endDate']) < time()) {
                $this->resp['warn'] = '使用时间已到期，请前往续费';
            } else {
                $company_user = query('company_user', "userId = '$uid' and coid = '$company[coid]'");
                if (empty($company_user['id'])) {
                    $warn = static::insert(array('table' => 'company_user', 'field' => ['id' => suiji(), 'coid' => $company['coid'], 'userId' => $uid]));
                    if ($warn == '新增成功') {
                        $this->code = 1;
                        $this->resp['data'] = [
                            'url' => $company['domain'] . '/interface/v1/index?',
                            'shortUrl' => $company['domain'] . '/'
                        ];
                    } else {
                        $this->resp['warn'] = '获取服务地址失败';
                    }
                } else {
                    $this->code = 1;
                    $this->resp['data'] = [
                        'url' => $company['domain'] . '/interface/v1/index?',
                        'shortUrl' => $company['domain'] . '/'
                    ];
                }
            }
        }
    }

    /**
     * 获取服务地址
     */
    public function getServerUrl()
    {
        $uid = $this->post('uid');//其他企业的人员请求雨木科技企业产生的加密userId
        if (empty($uid)) {
            $this->resp['warn'] = '未找到uid';
        }else{
            $company_user = query('company_user', "userId = '$uid'");
            if (empty($company_user['id'])) {
                $this->resp['warn'] = '获取服务地址失败';
            } else {
                $company = query('company', "coid = '$company_user[coid]'");
                $this->code = 1;
                $this->resp['data'] = [
                    'url' => $company['domain'] . '/interface/v1/index?',
                    'shortUrl' => $company['domain'] . '/'
                ];
            }
        }
    }

    public function main(){
        $this->init();//常用参数
        $act = $this->get('fun');
        if (method_exists($this, $act)) {
            $this->$act();
        } else {
            echo '<h2>Modules don\'t exist</h2>';
        }
    }
}
exit((new wechatInitClass())->main());

